#region

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;


using Volo.Abp;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;

#endregion

namespace Leo.Core.Swagger
{
    [DependsOn(typeof(AbpSwashbuckleModule))]
    public class SwaggerModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            var authority = configuration["AuthServer:Authority"];
            var apiDocOptions = configuration.GetSection(ApiDocOptions.ApiDoc).Get<ApiDocOptions>();
            if (apiDocOptions != null && apiDocOptions.ApiList != null && apiDocOptions.ApiList.Count > 0)
            {
                var list = new List<OpenApiInfoEx>();
                foreach (var apiDoc in apiDocOptions.ApiList)
                {
                    list.Add(apiDoc.ToOpenApiInfo());
                }

                ApiConfig.AddApiInfoList(list);
            }
            else
            {
                ApiConfig.AddDefaultApiGroup();
            }
            ApiConfig.InitGroupList();
            context.Services.AddAbpSwaggerGen(options => options.BuildGen());
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "/swagger/{documentname}/swagger.json";
            });
            app.UseAbpSwaggerUI(options =>
            {
                options.BuildUI(app);
            });
        }
    }
}